<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="cancelBtn">
    <el-form :model="formData" label-width="120px">
      <el-form-item label="姓名">
        <el-input style="width: 50%" placeholder="请输入姓名" v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机">
        <el-input style="width: 50%" placeholder="请输入手机号" v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker
          style="width: 50%"
          placeholder="请选择入职时间"
          v-model="formData.timeOfEntry"
        />
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select style="width: 50%" placeholder="请选择" v-model="formData.formOfEmployment">
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号">
        <el-input style="width: 50%" placeholder="请输入工号" v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门">
        <el-input
          style="width: 50%"
          placeholder="请选择部门"
          v-model="formData.departmentName"
          @focus="getDepartments"
        ></el-input>
        <el-tree
          v-if="showTree"
          :data="treeData"
          default-expand-all=""
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          style="width: 50%"
          placeholder="请选择转正时间"
          v-model="formData.correctionTime"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cancelBtn">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employees';
import EmployeeEnum from '@/api/constant/employees';
import { getDepartMents } from '@/api/departments';
import { tranListToTreeData } from '@/utils';
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      EmployeeEnum: EmployeeEnum,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: '',
      },
      treeData: [],
      showTree: false,
    };
  },
  methods: {
    cancelBtn() {
      this.$emit('changeShowDialog');
      // this.$parent.showDialog = false;
      // this.$emit('update:showDialog', false);
    },
    async btnOk() {
      const res = await addEmployee(this.formData);
      console.log('Res:', res);
      this.$emit('changeShowDialog');
      this.formData = {};
      this.$parent.getEmployeeList();
    },
    async getDepartments() {
      this.showTree = true; // 控制显示和隐藏
      const res = await getDepartMents(); // 获取所有的部门的数据
      // depts是数组 但不是树形
      this.treeData = tranListToTreeData(res.data.data.depts, ''); // 将扁平化的数据转化为树形结构
    },
    selectNode(node) {
      this.formData.departmentName = node.name;
      this.showTree = false;
    },
  },
};
</script>

<style></style>
